<?php
//  This file is part of EkkinCMS.
//
//  EkkinCMS is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  EkkinCMS is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with EkkinCMS.  If not, see <http://www.gnu.org/licenses/>.

$root_path = "../";



if (isset($_POST["submit_donate"])){
    include_once("../includes/utils.php");
    $key = "PAYPALD1p23QazW%ED@S453%";

    if ($_POST["import"] == "Other"){
        $import = $_POST["import_other"];
    } else {
        $import = $_POST["import"];
    }

    $valuta = $_POST["valuta"];

    $mail = string_decrypt($_POST["paypal_id"], $key);
    $project = $_POST["project"];

    $button = "
    function paypal_submit(){
        document.forms[\"form_paypal\"].submit();

    }
    <br/>
    <form id=\"form_paypal\" action=\"https://www.paypal.com/cgi-bin/webscr\" method=\"post\" target=\"_top\">
    <!-- Identify your business so that you can collect the payments. -->
    <input name=\"business\" type=\"hidden\" value=\"$mail\" />
    <!-- Specify a Buy Now button. --> <input name=\"cmd\" type=\"hidden\" value=\"_xclick\" />
    <!-- Specify details about the item that buyers will purchase. -->
    <input name=\"item_name\" type=\"hidden\" value=\""._("One-time Donation")." ($project) \" />
    <input name=\"amount\" type=\"hidden\" value=\"$import\" />
    <input name=\"currency_code\" type=\"hidden\" value=\"$valuta\" />
    <!-- Display the payment button. -->
    <!--input alt=\"PayPal - The safer, easier way to pay online\" name=\"submit\" src=\"plugins/data/donate_button.png\" type=\"image\" />
    <input type=\"submit\" name=\"submit2\"/ -->
    <img src=\"https://www.paypal.com/en_US/i/scr/pixel.gif\" border=\"0\" alt=\"\" width=\"1\" height=\"1\" />
    <a href=\"javascript:paypal_submit()\">Click here to continue ...</a>
    </form>


    ";


    if ($_POST["name"] != ""){
        $name = $_POST["name"];
        $email = $_POST["email"];
        $phone = $_POST["phone"];
        $address = $_POST["address"];
        $amount = "$import $valuta";
        $dest = $_POST["dest"];
        $date = date('l jS \of F Y h:i:s A', time());
        $headers = "From: $name <$email>" . "\r\n" .
            'X-Mailer: PHP/' . phpversion();
        $message = "Membership details received:

        Please check with the amount and date if the donation is actually received by PayPal.

Name:       $name
Address:    $address
Phone:      $phone
Email:      $email
Amount:     $amount
Date:       $date

Dest:       $dest

";

        //echo $message;

        if (mail($dest, _("Incoming Paypal Donation"), $message, $headers)){

        } else {

        }

    }
    echo $button;



} else {

    include_once("ext_lib/simple_html_dom.php");

    function execute_paypal_donate($site_content){

        $html = str_get_html($site_content);

        foreach($html->find("a") as $element){
            if ($element->class == "ekkin"){
                if ($element->id == "paypal_donate"){
                    $mail = $element->innertext;
                    $mail2 = str_replace("mailto:", "", $element->href);
                    $element -> outertext = generate_donate($mail, $mail2);
                }

            }
        }


        return $html->save();
    }

}

function generate_donate($mail, $mail2){
    //
    $key = "PAYPALD1p23QazW%ED@S453%";

    $mail = string_encrypt($mail, $key);

    $text = "
Select the quantity to donate:
<script type='text/javascript' src='${relpath}ext_lib/webtoolkit.aim.js'></script>
<script language=\"javascript\">
function startCallback() {
    // make something useful before submit (onStart)
    return true;
}

function completeCallback(response) {
    // make something useful after (onComplete)
    // document.getElementById('nr').innerHTML = parseInt(document.getElementById('nr').innerHTML) + 1;
    document.getElementById('response').innerHTML = response;
    paypal_submit();

}

function paypal_submit(){
    document.forms[\"form_paypal\"].submit();

}
</script>
<form method='POST' name='form_donate' action='plugins/paypal_donate.php' onsubmit='return AIM.submit(this, {onStart : startCallback, onComplete : completeCallback})'>
    <table>
        <tr>
            <td> <input type=\"radio\" name=\"import\" value=\"10\" onclick=\"this.form.import_other.disabled=true\"/> 10 </td>
            <td> <input type=\"radio\" name=\"import\" value=\"20\" onclick=\"this.form.import_other.disabled=true\"/> 20</td>
            <td> <input type=\"radio\" name=\"import\" value=\"50\" onclick=\"this.form.import_other.disabled=true\" checked/> 50</td>
            <td> <input type=\"radio\" name=\"import\" value=\"100\" onclick=\"this.form.import_other.disabled=true\"/> 100</td>
        </tr>
        <tr>
            <td> <input type=\"radio\" name=\"import\" value=\"200\" onclick=\"this.form.import_other.disabled=true\"/> 200 </td>
            <td> <input type=\"radio\" name=\"import\" value=\"500\" onclick=\"this.form.import_other.disabled=true\"/> 500</td>
            <td colspan=\"2\"> <input type=\"radio\" name=\"import\" value=\"1000\" onclick=\"this.form.import_other.disabled=true\"/> 1,000</td>

        </tr>
        <tr>
            <td> <input type=\"radio\" name=\"import\" value=\"2000\" onclick=\"this.form.import_other.disabled=true\"/> 2,000 </td>
            <td> <input type=\"radio\" name=\"import\" value=\"5000\" onclick=\"this.form.import_other.disabled=true\"/> 5,000</td>
            <td colspan=\"2\"> <input type=\"radio\" name=\"import\" value=\"10000\" onclick=\"this.form.import_other.disabled=true\"/> 10,000</td>
        </tr>
        <tr>
            <td colspan=\"4\">
                <input type=\"radio\" name=\"import\" value=\"Other\" onclick=\"this.form.import_other.disabled=false; this.form.import_other.focus();\"/> Other:
                <input type=\"text\" name=\"import_other\" size=\"5\" disabled=\"disabled\"/>
                <select name=\"valuta\">
                    <option value=\"USD\">"._("US Dollars")." - \$USD</option>
                    <option value=\"MXN\">"._("Mexican Pesos")." - \$MXN</option>
                </select>
            </td>
        </tr>
        <tr>
            <td colspan=\"4\">
                Project: <select name=\"project\">
                    <option value=\""._("Organization")."\"/>"._("Organization in general")."</option>
                    <option value=\""._("VRR")."\"/>"._("Veracruz River of Raptors")."</option>

                </select>
            </td>
        </tr>
    </table>



    <input type=\"hidden\" name=\"paypal_id\" value=\"$mail\"/>
    <input type=\"hidden\" name=\"dest\" value=\"$mail2\"/>
<p>

    "._("Optionally you can enter the following details if your want to become member and receive our semiannual newspaper").":<br/>
    <table>
        <tr>
            <td> "._("Name").":</td>
            <td><input type=\"text\" name=\"name\"/></td>
        </tr>
        <tr>
            <td>"._("Address").":</td>
            <td><input type=\"text\" name=\"address\"/></td>
        </tr>
        <tr>
            <td>"._("Email").":</td>
            <td><input type=\"text\" name=\"email\"/></td>
        </tr>
        <tr>
            <td>"._("Phone").":</td>
            <td><input type=\"text\" name=\"phone\"/></td>
        </tr>
    </table>

</p>
<input type=\"submit\" name=\"submit_donate\" value=\""._("Donate using PayPal")."\"/>
</form><div id=\"response\"></div>
    ";
    return $text;
}


?>
